//
//  JYHomeViewCell.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/4.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit
import SDWebImage
import HYLabel

private let edgeMargin : CGFloat = 15
private let itemMargin : CGFloat = 10

class JYHomeViewCell: UITableViewCell {
    // MARK:- 控件属性
    @IBOutlet weak var contentLableWCons: NSLayoutConstraint!
    @IBOutlet weak var picViewHCons: NSLayoutConstraint!
    @IBOutlet weak var picViewWCons: NSLayoutConstraint!
    @IBOutlet weak var picViewBottomCons: NSLayoutConstraint!
    @IBOutlet weak var retweetedContentLabelTopCons: NSLayoutConstraint!
    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var verifiedView: UIImageView!
    @IBOutlet weak var vipView: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: HYLabel!
    @IBOutlet weak var picView: JYPicCollectionView!
    @IBOutlet weak var retweetedContentLabel: HYLabel!
    @IBOutlet weak var retweetedBgView: UIView!
    @IBOutlet weak var bottomToolView: UIView!
    
    
    // MARK:- 自定义属性
    var viewModel : JYStatusViewModel? {
        didSet {
            //nil值校验
            guard let viewModel = viewModel else {
                return
            }
            //设置数据
            iconView.sd_setImageWithURL(viewModel.profileUrl, placeholderImage: UIImage(named: "avatar_default_small"))
            nameLabel.text = viewModel.status?.user?.name
            nameLabel.textColor = viewModel.vipImage == nil ? UIColor.blackColor() : UIColor.orangeColor()
            verifiedView.image = viewModel.verifiedImage
            vipView.image = viewModel.vipImage
            timeLabel.text = viewModel.createdAtText
            contentLabel.attributedText = FindEmoticon.shareInstance.findAttrString(viewModel.status?.text, font: contentLabel.font)
            if let source = viewModel.sourceText {
                sourceLabel.text = "来自" + source
            } else {
                sourceLabel.text = nil
            }
            
            //计算picView宽度和高度的约束
            let picViewSize = colculatePicViewSize(viewModel.picURLs.count)
            picViewWCons.constant = picViewSize.width
            picViewHCons.constant = picViewSize.height
            
            //将picURLs传递给picView
            picView.picURLs = viewModel.picURLs
            
            //设置转发微博的正文
            if viewModel.status?.retweeted_status != nil { //有转发微博
                //设置转发微博的正文
                if let name = viewModel.status?.retweeted_status?.user?.name, retweetedText = viewModel.status?.retweeted_status?.text {
                    
                    let retweetedText = "@" + "\(name)：" + retweetedText
                    retweetedContentLabel.attributedText = FindEmoticon.shareInstance.findAttrString(retweetedText, font: retweetedContentLabel.font)
                }
                
                //设置背景显示
                retweetedBgView.hidden = false
                
                //设置转发正文距离顶部的约束
                retweetedContentLabelTopCons.constant = 15
                
            } else { //没有转发微博
                retweetedContentLabel.text = nil
                retweetedBgView.hidden = true
                retweetedContentLabelTopCons.constant = 0
            }
            
            //计算cell的高度
            //强制布局
            layoutIfNeeded()
            //获取底部工具条最大Y值给cellHeight
            if viewModel.cellHeight == 0 {
                viewModel.cellHeight = CGRectGetMaxY(bottomToolView.frame)
            }
        }
    }
    
    
    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        
        //设置正文宽度约束
        contentLableWCons.constant = UIScreen.mainScreen().bounds.width - 2 * edgeMargin
        
        //设置HYLabel的内容
        contentLabel.matchTextColor = UIColor.purpleColor()     //可以修改文字的颜色
        retweetedContentLabel.matchTextColor = UIColor.purpleColor()
        
        //监听HYLabel内容的点击
        contentLabel.userTapHandler = { (label, user, range) in
            print(user)
            print(range)
        }
        
        // 监听链接的点击
        contentLabel.linkTapHandler = { (label, link, range) in
            print(link)
            print(range)
        }
        
        // 监听话题的点击
        contentLabel.topicTapHandler = { (label, topic, range) in
            print(topic)
            print(range)
        }
    }

}

// MARK:- 计算函数
extension JYHomeViewCell {
    private func colculatePicViewSize(count : Int) -> CGSize {
        //没有配图
        if count == 0 {
            picViewBottomCons.constant = 0
        
            return CGSizeZero
        }
        
        //有配图设置约束
        picViewBottomCons.constant = 10
        
        //设置picView的布局
        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
        
        //单张配图
        if count == 1 {
            
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(viewModel?.picURLs.first?.absoluteString)
            
            //一张图片时layout的itemSize
            layout.itemSize = CGSize(width: image.size.width * 2, height: image.size.height * 2)
            
            return CGSize(width: image.size.width * 2, height: image.size.height * 2)
        }
        
        //计算imageView的宽高
        let imageViewWH = (UIScreen.mainScreen().bounds.width - 2 * edgeMargin - 2 * itemMargin) / 3
        
        //其他图片时layout的itemSize
        layout.itemSize = CGSize(width: imageViewWH, height: imageViewWH)
        
        //4张配图
        if count == 4 {
            let picViewWH = 2 * imageViewWH + itemMargin + 1
            return CGSize(width: picViewWH, height: picViewWH)
        }
        
        //其他张配图
        //计算行数
        let rows = CGFloat((count - 1) / 3 + 1)
        
        //计算picView的高度
        let picViewH = rows * imageViewWH + (rows - 1) * itemMargin
        
        //计算picView的宽度
        let picViewW = UIScreen.mainScreen().bounds.width - 2 * edgeMargin
        
        return CGSize(width: picViewW, height: picViewH)
    }
}
